/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2014年12月29日         luwei
 */

package com.coocaa.launcher.sidebar.left;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ScrollView;

import com.coocaa.launcher.framework.sidebar.ContainerLayout;
import com.coocaa.launcher.pattern.normal.UIConfig;
import com.coocaa.launcher.pattern.normal.hotseat.FocusView;
import com.coocaa.x.uipackage.SkinManager;
import com.tianci.appstore.R;
import com.tianci.appstore.utils.SkyConstants;

public class LeftSideContainerLayout extends ContainerLayout {
    private ScrollView mScrollView = null;
    private LinearLayout mContainerLayout = null;
    protected FrameLayout mTempLayout = null;
    private FocusView mFocusView = null;

    @Override
    public void destory() {

    }

    public LeftSideContainerLayout(Context context) {
        super(context);
        addTempLayout();
        addFocusView();
        addScrollView();
        addContainerLayout();
    }

    @SuppressLint("ResourceAsColor")
    private void addTempLayout() {
        mTempLayout = new FrameLayout(mContext);
        LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
        SkinManager.getInstance().setBackgroundColor(mTempLayout, R.color.left_side_background_color);
        addView(mTempLayout, params);
    }

    private void addFocusView() {
        mFocusView = new FocusView(mContext);
        mFocusView.setFocusable(false);
        SkinManager.getInstance().setBackgroundResource(mFocusView, R.drawable.hot_seat_focus_bg);
        mTempLayout.addView(mFocusView);
    }

    private void addScrollView() {
        mScrollView = new ScrollView(mContext);
        LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
        mTempLayout.addView(mScrollView, params);
    }

    private void addContainerLayout() {
        mContainerLayout = new LinearLayout(mContext);
        mContainerLayout.setGravity(Gravity.CENTER_VERTICAL);
        mContainerLayout.setOrientation(LinearLayout.VERTICAL);
        LayoutParams params = new LayoutParams(getItemWidth(),
                LayoutParams.MATCH_PARENT, Gravity.CENTER_VERTICAL);
        mScrollView.addView(mContainerLayout, params);
    }

    AnimationListener mInListener = new AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {
            // TODO Auto-generated method stub
            mContainerLayout.getChildAt(0).requestFocus();
            mFocusView.setVisibility(View.VISIBLE);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onAnimationEnd(Animation animation) {
            mContainerLayout.getChildAt(0).requestFocus();
        }
    };

    @Override
    protected Animation getShownAnimation() {
        Animation animation = AnimationUtils.loadAnimation(mContext, R.anim.in_leftright);
        animation.setAnimationListener(mInListener);
        animation.setDuration(UIConfig.HotSeatShowAnimDuration);
        return animation;
    }

    @Override
    protected Animation getHidenAnimation() {
        Animation animation = AnimationUtils.loadAnimation(mContext, R.anim.out_rightleft);
        animation.setDuration(UIConfig.HotSeatShowAnimDuration);
        return animation;
    }

    @Override
    public ViewGroup getContainerLayout() {
        return mContainerLayout;
    }

    @Override
    public void show() {
        super.show();

    }

    @Override
    public void addExtraView(ViewGroup.LayoutParams params, View view, Animation anim) {
        if (anim != null)
            mTempLayout.startAnimation(anim);
        addView(view, params);
    }

    @Override
    public void removeExtraVeiw(View view, Animation anim) {
        if (anim != null)
            mTempLayout.startAnimation(anim);
        removeView(view);

    }

    @Override
    public FocusView getFocusView() {
        return mFocusView;
    }

    @Override
    public void showFocusView(boolean show) {
        if (show) {
            mFocusView.setVisibility(View.VISIBLE);
        } else {
            mFocusView.setVisibility(View.GONE);
        }
    }

    public int getItemWidth() {
        return SkyConstants.Div(501);
    }

    @Override
    public void setBackgroundColor(int id) {
        SkinManager.getInstance().setBackgroundColor(mTempLayout, id);
    }
}
